<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div>
        <label>Number A:<input id="radio-a" type="radio" name="math-obj" value="a"></label><input id="num-a" type="text">
        <label>Number B:<input id="radio-b" type="radio" name="math-obj" value="b"></label><input id="num-b" type="text">
    </div>
    <div>
        <button>判断当前选中的输入框输入内容是否为数字</button>
        <button>把 A 四舍五入为 B 个小数位数的数字</button>
        <button>当前选中数字的绝对值</button>
        <button>对当前选中的数字进行上舍入</button>
        <button>对当前选中的数字进行下舍入</button>
        <button>把当前选中的数字四舍五入为最接近的整数</button>
        <button>返回 A 和 B 中的最高值</button>
        <button>返回 A 和 B 中的最低值</button>
    </div>
    <p id="result"></p>
    <script>
        var buttons = document.querySelectorAll('button');
        var p = document.getElementById('result');
        buttons[0].onclick = function() {
            var objs = document.getElementsByName('math-obj');
            for (let i = 0, len = objs.length; i < len; i++) {
                if (objs[i].checked === true) {
                    let value = document.getElementById('num-' + objs[i].value).value;
                    if (isNaN(value)) {
                        p.innerHTML = '不是数字！';
                    } else {
                        p.innerHTML = '是数字！';
                    }
                }
            }
        }
        buttons[1].onclick = function() {
            let num1 = document.getElementById('num-a').value;
            let num2 = document.getElementById('num-b').value;
            if (isNaN(num1) || isNaN(num2)) {
                return console.log('不是数字！');
            }
            if (num2 < 0 || num2 > 100) {
                return console.log('b不得小于0大于100！');
            }
            p.innerHTML = Number(num1).toFixed(num2);
        }

        buttons[2].onclick = function() {
            var objs = document.getElementsByName('math-obj');
            for (let i = 0, len = objs.length; i < len; i++) {
                if (objs[i].checked === true) {
                    let value = document.getElementById('num-' + objs[i].value).value;
                    if (isNaN(value)) {
                        return console.log('不是数字！');
                    }
                    p.innerHTML = Math.abs(value);
                }
            }
        }

        buttons[3].onclick = function() {
            var objs = document.getElementsByName('math-obj');
            for (let i = 0, len = objs.length; i < len; i++) {
                if (objs[i].checked === true) {
                    let value = document.getElementById('num-' + objs[i].value).value;
                    if (isNaN(value)) {
                        return console.log('不是数字！');
                    }
                    p.innerHTML = Math.ceil(value);
                }
            }
        }

        buttons[4].onclick = function() {
            var objs = document.getElementsByName('math-obj');
            for (let i = 0, len = objs.length; i < len; i++) {
                if (objs[i].checked === true) {
                    let value = document.getElementById('num-' + objs[i].value).value;
                    if (isNaN(value)) {
                        return console.log('不是数字！');
                    }
                    p.innerHTML = Math.floor(value);
                }
            }
        }
        buttons[5].onclick = function() {
            var objs = document.getElementsByName('math-obj');
            for (let i = 0, len = objs.length; i < len; i++) {
                if (objs[i].checked === true) {
                    let value = document.getElementById('num-' + objs[i].value).value;
                    if (isNaN(value)) {
                        return console.log('不是数字！');
                    }
                    p.innerHTML = Math.round(value);
                }
            }
        }

        buttons[6].onclick = function() {
            let num1 = document.getElementById('num-a').value;
            let num2 = document.getElementById('num-b').value;
            if (isNaN(num1) || isNaN(num2)) {
                return console.log('不是数字！');
            }
            p.innerHTML = Math.max(num1, num2);
        }

        buttons[7].onclick = function() {
            let num1 = document.getElementById('num-a').value;
            let num2 = document.getElementById('num-b').value;
            if (isNaN(num1) || isNaN(num2)) {
                return console.log('不是数字！');
            }
            p.innerHTML = Math.min(num1, num2);
        }
    </script>
</body>

</html>